package com.gzw.dms.activity;

import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.PopupMenu;

import com.google.gson.reflect.TypeToken;
import com.gzw.dms.MyApplication;
import com.gzw.dms.R;
import com.gzw.dms.adapter.CustomerAdapter;
import com.gzw.dms.adapter.DriverAdapter;
import com.gzw.dms.base.BaseActivity;
import com.gzw.dms.bean.CuotomerInfo;
import com.gzw.dms.bean.DriverInfo;
import com.gzw.dms.bean.UserInfo;
import com.gzw.dms.utils.HttpUtils;
import com.gzw.dms.utils.JsonUtil;
import com.gzw.dms.utils.T;
import com.gzw.dms.utils.UrlUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

public class SelectCustomerActivity extends BaseActivity {
    private ListView lv_driver;
    private Context mContext;
    private ProgressDialog mProgressDialog;
    private Toolbar toolbar;
    private ArrayList<String> ls_map;
    private Intent mIntetn;
    private ArrayList<CuotomerInfo> cuotomerInfos;
    private String row = "0";
    private String page  = "50";
    private String customer_name ="";
    private String customer_tel ="";
    private String customer_address="";

    private SwipeRefreshLayout mSwipeLayout;
    private int refresh = 0;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    protected void setContentView() {
        setContentView(R.layout.activity_select_customer2);

    }

    @Override
    protected void initViews() {
        toolbar = (Toolbar) findViewById(R.id.toolbar);
        lv_driver = (ListView) findViewById(R.id.lv_driver);
        mSwipeLayout = (SwipeRefreshLayout) findViewById(R.id.id_swipe_ly);
    }

    @Override
    protected void initData() {
        mContext = this;
        toolbar.setTitle("选择项目部");
        setSupportActionBar(toolbar);
        mIntetn=getIntent();
        setSupportActionBar(toolbar);
        ShowOrderTask showOrderTask = new ShowOrderTask();
        showOrderTask.execute(row,page,customer_name,customer_tel,customer_address);
    }

    @Override
    protected void initEvents() {
        lv_driver.setOnItemClickListener(new mOnItemClick());
        toolbar.setNavigationOnClickListener(new mOnClick());
        mSwipeLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                refresh = 1;
                ShowOrderTask showOrderTask = new ShowOrderTask();
                showOrderTask.execute(row,page,customer_name,customer_tel,customer_address);

            }
        });

    }

    public class ShowOrderTask extends AsyncTask<String, Void, String> {
        //onPreExecute方法用于在执行后台任务前做一些UI操作
        @Override
        protected void onPreExecute() {
            if (refresh == 0) {
//                mSwipeLayout.setRefreshing(true);
                mProgressDialog = ProgressDialog.show(mContext, null, "正在加载中...");
                mProgressDialog.setCancelable(true);
                mProgressDialog.setCanceledOnTouchOutside(false);

            } else if (refresh == 1) {
                mSwipeLayout.setRefreshing(true);
            }
        }

        protected String doInBackground(String... params) {
            Map<String, Object> map = new HashMap<>();
            UserInfo userInfo = MyApplication.getUserInfo();
            map.put("userInfo", userInfo);
            map.put("row",params[0]);
            map.put("page",params[1]);
            map.put("customer_name",params[2]);
            map.put("customer_tel",params[3]);
            map.put("customer_address",params[4]);

            String json = JsonUtil.toJson(map);
            return HttpUtils.doPost(UrlUtils.getInstance().getJavaUrl("appAdminSearchCustomerInfo"), "json=" + json);
        }

        @Override
        protected void onPostExecute(final String result) {
            if (refresh == 0) {
//                mSwipeLayout.setRefreshing(false);
                mProgressDialog.dismiss();

            } else if (refresh == 1) {
                mSwipeLayout.setRefreshing(false);
            }
            if (isNull(result)) {
                T.showShort(mContext, "连接服务器异常");
            } else {
                if (result.indexOf("errorMsg") > 0) {
                    String errorMsg = JsonUtil.getMsg(result, "errorMsg");
                    T.showShort(mContext, errorMsg);
                } else {
                    String successMsg = JsonUtil.getMsg(result, "successMsg");
                    cuotomerInfos = JsonUtil.getPersons(successMsg, new TypeToken<ArrayList<CuotomerInfo>>() {
                    }.getType());
                    CustomerAdapter customerAdapter = new CustomerAdapter(mContext,cuotomerInfos);
                    lv_driver.setAdapter(customerAdapter);
                }
            }
        }
    }


    class mOnItemClick implements AdapterView.OnItemClickListener {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            String customer_id = cuotomerInfos.get(position).getCustomer_id();
            String customer_name = cuotomerInfos.get(position).getCustomer_name();
            Intent mIntetn1 = new Intent();
            mIntetn1.setClass(mContext, QscxOrderActivity1.class);
            mIntetn1.putExtra("type", "3");
            mIntetn1.putExtra("customer_id",customer_id);
            mIntetn1.putExtra("customer_name",customer_name);
            startActivityForResult(mIntetn1, 1);

        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu_home; this adds items to the action bar if it is present.
//        getMenuInflater().inflate(R.menu.menu_vehicle, menu);
        getMenuInflater().inflate(R.menu.menu_select, menu);
        return true;
    }
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.item_select:
                Intent myIntetn = new Intent();
                myIntetn.putExtra("fpstste","1");
                myIntetn.setClass(mContext, CustomerTJActivity.class);
                startActivityForResult(myIntetn, 2);
                break;
        }
        return super.onOptionsItemSelected(item);
    }

    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == 2) {
            refresh = 0;
            String customer_name = (String) data.getExtras().get("customer_name");
            String customer_tel = (String) data.getExtras().get("customer_tel");
            String customer_address = (String) data.getExtras().get("customer_address");

            ShowOrderTask showOrderTask = new ShowOrderTask();
            showOrderTask.execute(row,page,customer_name,customer_tel,customer_address);

        }

    }
    /**
     * Created by Wangzhiqiang on 2015/11/19.
     * button的点击方法
     */
    class mOnClick implements View.OnClickListener {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            switch (v.getId()) {

                default:
                    Intent mIntent2 = new Intent();
                    //把返回数据存入Intent
                    mIntent2.putExtra("result", 1);
                    //设置返回数据
                    setResult(3, mIntent2);
                    finish();
                    break;
            }
        }
    }

}